//
//  GMArtworkInfoView.m
//  gentleman
//
//  Created by 范月成 on 2017/12/6.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMArtworkInfoView.h"
#import "VendorHeader.h"
#import "ClassesHeader.h"

@interface GMArtworkInfoItem ()

@end

@implementation GMArtworkInfoItem

- (instancetype)init {
    if (self = [super init]) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.backgroundColor = [UIColor whiteColor];
 
    [self addSubview:self.titleLabel];
    [self addSubview:self.tagLabel];
}

#pragma mark - getter
- (YYLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [GMUIHelper generalTitleLabel];
    }
    return _titleLabel;
}

- (YYLabel *)tagLabel {
    if (!_tagLabel) {
        _tagLabel = [GMUIHelper generalTitleLabel];
        _tagLabel.numberOfLines = 0;
    }
    return _tagLabel;
}

- (void)setGalleryTag:(GMGalleryTag *)galleryTag {
    _galleryTag = galleryTag;
    self.titleLabel.text = galleryTag.name;
    
    NSMutableArray *tagsArray = [NSMutableArray array];
    NSMutableString *text = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i<galleryTag.tags.count; i++) {
        NSDictionary *tagInfo = galleryTag.tags[i];
        [tagsArray addObject:tagInfo[@"title"]];
        [text appendString:[NSString stringWithFormat:@"%@    ", tagInfo[@"title"]]];
    }
    
    NSMutableAttributedString *attriText = [[NSMutableAttributedString alloc] initWithString:text];
    
    for (NSInteger i = 0; i<tagsArray.count; i++) {
        [attriText yy_setTextHighlight:[YYTextHighlight new] range:[text rangeOfString:tagsArray[i]]];
    }

    attriText.yy_font = kSystemBoldFont(16);
    attriText.yy_color = [UIColor themeBlack];
    attriText.yy_lineSpacing = 5;
    
    self.tagLabel.attributedText = attriText;
    @weakify(self)
    self.tagLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        @strongify(self)
        NSString *tag = [NSString stringWithFormat:@"%@:%@", self.titleLabel.text, [text.string substringWithRange:range]];
        !self.tagTapBlock?:self.tagTapBlock([tag stringByURLEncode]);
    };
}

- (void)setTitle:(NSString *)title
          detail:(NSString *)detail {
    self.titleLabel.text = title;
    self.tagLabel.text = detail?detail:@"None";
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self);
        make.width.mas_equalTo(100);
    }];
    
    [self.tagLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(12);
        make.right.equalTo(self.mas_right).offset(-12);
        make.top.equalTo(self);
        
        CGFloat h = 0;
        if (self.tagLabel.attributedText) {
            h = [self.tagLabel.attributedText.string boundingRectWithSize:CGSizeMake(kScreenWidth-126, HUGE) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:self.tagLabel.attributedText.yy_attributes context:nil].size.height;
        } else {
            h = [self.tagLabel.text heightForFont:kSystemFont(16) width:kScreenWidth-126];
        }
        make.height.mas_equalTo(h);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.tagLabel.mas_bottom);
    }];
}

@end

@interface GMArtworkInfoView ()

@property (nonatomic, strong) UIActivityIndicatorView *indicator;
@property (nonatomic, strong) UIButton *refreshButton;

@end

@implementation GMArtworkInfoView

- (instancetype)init {
    if (self = [super init]) {
        [self addSubview:self.indicator];
        [self addSubview:self.refreshButton];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.indicator];
    }
    return self;
}

- (void)configUIWithArtwork:(GMArtwork *)artwork
                    gallery:(GMGallery *)gallery {
    _artwork = artwork;
    _gallery = gallery;
    
    [self.indicator stopAnimating];
    //preview
    //title
    YYLabel *titlePreview = [GMUIHelper generalTitleLabel];
    titlePreview.text = @"预览";
    [self addSubview:titlePreview];
    [titlePreview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.left.equalTo(self).offset(12);
        make.height.mas_equalTo(30);
    }];
    //image
    UIScrollView *imageContainer = [[UIScrollView alloc] init];
    [self addSubview:imageContainer];
    [imageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titlePreview.mas_bottom).offset(5);
        make.left.equalTo(self);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(100);
    }];
    
    UIImageView *imagePreview = [[UIImageView alloc] init];
    imagePreview.userInteractionEnabled = YES;
    [imageContainer addSubview:imagePreview];
    [imagePreview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.height.equalTo(imageContainer);
    }];
    @weakify(self)
    [imagePreview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        !self.thumbTapBlock?:self.thumbTapBlock(imagePreview);
    }]];
    UIView *imgSeparator = [self separator];
    [self addSubview:imgSeparator];
    [imgSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageContainer.mas_bottom).offset(5);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(kScreenWidth-12);
        make.height.mas_equalTo(0.5);
    }];
    __weak UIImageView *blockImageView = imagePreview;
    __weak UIScrollView *blockContainer = imageContainer;
    [imagePreview sd_setImageWithURL:[NSURL URLWithString:gallery.thumbnail] placeholderImage:nil options:kNilOptions progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            [blockImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(image.size.width/image.size.height*100);
            }];
            
            [blockContainer mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.equalTo(blockImageView.mas_right);
            }];
        }
    }];
    //tags
    //title
    YYLabel *titleTag = [GMUIHelper generalTitleLabel];
    titleTag.text = @"标签";
    [self addSubview:titleTag];
    [titleTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgSeparator.mas_bottom).offset(5);
        make.left.equalTo(self).offset(12);
        make.height.mas_equalTo(30);
    }];
    //tags
    GMArtworkInfoItem *lastTag = nil;
    for (NSInteger i = 0; i<gallery.tagArr.count; i++) {
        GMArtworkInfoItem *item = [[GMArtworkInfoItem alloc] init];
        item.galleryTag = gallery.tagArr[i];
        @weakify(self)
        item.tagTapBlock = ^(NSString *tag) {
            @strongify(self)
            !self.tagTapBlock?:self.tagTapBlock(tag);
        };
        [self addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.width.mas_equalTo(kScreenWidth);
            if (lastTag) {
                make.top.equalTo(lastTag.mas_bottom).offset(5);
            } else {
                make.top.equalTo(titleTag.mas_bottom).offset(5);
            }
        }];
        lastTag = item;
    }
    UIView *tagSeparator = [self separator];
    [self addSubview:tagSeparator];
    [tagSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastTag.mas_bottom).offset(5);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(kScreenWidth-12);
        make.height.mas_equalTo(0.5);
    }];
    //info
    YYLabel *titleInfo = [GMUIHelper generalTitleLabel];
    titleInfo.text = @"信息";
    [self addSubview:titleInfo];
    [titleInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tagSeparator.mas_bottom).offset(5);
        make.left.equalTo(self).offset(12);
        make.height.mas_equalTo(30);
    }];
    
    NSDictionary *artTitle = [self infoWithTitle:@"标题" detail:self.artwork.title];
    NSDictionary *artJPTitle = [self infoWithTitle:@"日语标题" detail:self.artwork.title_jpn];
    NSDictionary *artCategory = [self infoWithTitle:@"分类" detail:self.artwork.category];
    NSDictionary *artUploader = [self infoWithTitle:@"上传者" detail:self.artwork.uploader];
    NSDictionary *artUploaderTime = [self infoWithTitle:@"上传时间" detail:self.gallery.info.posted];
    NSDictionary *artParent = [self infoWithTitle:@"父画廊" detail:self.gallery.info.parent];
    NSDictionary *artVisible = [self infoWithTitle:@"可见性" detail:self.gallery.info.visible];
    NSDictionary *artLanguage = [self infoWithTitle:@"语言" detail:self.gallery.info.language];
    NSDictionary *artSize = [self infoWithTitle:@"大小" detail:self.gallery.info.size];
    NSDictionary *artPage = [self infoWithTitle:@"页数" detail:self.gallery.info.length];
    NSDictionary *artFavorited = [self infoWithTitle:@"收藏数" detail:self.gallery.info.favorited];
    GMArtworkInfoItem *lastInfo = nil;
    NSArray *infos = @[artTitle, artJPTitle, artCategory, artUploader, artUploaderTime, artParent, artVisible, artLanguage, artSize, artPage, artFavorited];
    for (NSInteger i = 0; i<infos.count; i++) {
        GMArtworkInfoItem *item = [[GMArtworkInfoItem alloc] init];
        NSDictionary *infoData = infos[i];
        [item setTitle:infoData[@"title"] detail:infoData[@"detail"]];
        [self addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.width.mas_equalTo(kScreenWidth);
            if (lastInfo) {
                make.top.equalTo(lastInfo.mas_bottom).offset(5);
            } else {
                make.top.equalTo(titleInfo.mas_bottom).offset(5);
            }
        }];
        lastInfo = item;
    }
    UIView *infoSeparator = [self separator];
    [self addSubview:infoSeparator];
    [infoSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastInfo.mas_bottom).offset(5);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(kScreenWidth-12);
        make.height.mas_equalTo(0.5);
    }];
    //类似画廊
    UIButton *similarArt = [GMUIHelper buttonWithFont:kSystemFont(16) titleColor:[UIColor themeBlack]];
    [similarArt setTitle:@"类似画廊" forState:UIControlStateNormal];
    [similarArt addTarget:self action:@selector(similarArtAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:similarArt];
    [similarArt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(infoSeparator.mas_bottom);
        make.left.equalTo(self);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(40);
    }];
    UIView *similarSeparator = [self separator];
    [self addSubview:similarSeparator];
    [similarSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(similarArt.mas_bottom);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(kScreenWidth-12);
        make.height.mas_equalTo(0.5);
    }];
    //其他
    UIButton *otherArt = [GMUIHelper buttonWithFont:kSystemFont(16) titleColor:[UIColor themeBlack]];
    [otherArt setTitle:@"上传者其他画廊" forState:UIControlStateNormal];
    [otherArt addTarget:self action:@selector(otherArtAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:otherArt];
    [otherArt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(similarArt.mas_bottom);
        make.left.equalTo(self);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(40);
    }];
    UIView *otherSeparator = [self separator];
    [self addSubview:otherSeparator];
    [otherSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(otherArt.mas_bottom);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(kScreenWidth-12);
        make.height.mas_equalTo(0.5);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(otherSeparator.mas_bottom).offset(12);
    }];
}

- (NSDictionary *)infoWithTitle:(NSString *)title
                         detail:(NSString *)detail {
    NSString *detailStr = nil;
    if (detail) {
        detailStr = detail;
    } else {
        detailStr = @"None";
    }
    return @{@"title":title, @"detail":detailStr};
}

- (UIView *)separator {
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor themeLightgory];
    return line;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.indicator.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
}

#pragma mark - action
- (void)refresh:(UIButton *)sender {
    sender.hidden = YES;
    self.indicator.hidden = NO;
    [self.indicator startAnimating];
    !self.refreshBlock?:self.refreshBlock();
}

- (void)similarArtAction:(UIButton *)sender {
    !self.similarArtBlock?:self.similarArtBlock();
}

- (void)otherArtAction:(UIButton *)sender {
    !self.otherUploadBlock?:self.otherUploadBlock();
}

#pragma mark - getter
- (UIActivityIndicatorView *)indicator {
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicator.hidesWhenStopped = YES;
        [_indicator startAnimating];
    }
    return _indicator;
}

- (UIButton *)refreshButton {
    if (!_refreshButton) {
        _refreshButton = [GMUIHelper buttonWithFont:kSystemFont(14) titleColor:[UIColor themeGray]];
        _refreshButton.hidden = YES;
        [_refreshButton setTitle:@"点击刷新" forState:UIControlStateNormal];
        [_refreshButton addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refreshButton;
}

@end
